package com.haoshuang.zhouzhoubang.ui.activity

import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import com.google.gson.Gson
import com.haoshuang.zhouzhoubang.databinding.ActivityServiceLog3Binding
import com.haoshuang.zhouzhoubang.model.bean.Image42Bean
import com.haoshuang.zhouzhoubang.ui.adapter.ImageAdapter24
import com.haoshuang.zhouzhoubang.ui.vm.ServiceLog3ViewModel
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.core.setOnItemClick
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger


/**
 *  
 */
class ServiceLog3Activity : BaseVMActivity<ServiceLog3ViewModel, ActivityServiceLog3Binding>() {

    override fun initView(savedInstanceState: Bundle?) {
        binding.appBar.ivBack.clickWithTrigger { finish() }
        binding.appBar.tvTitle.text = "服务日志"
    }
    var list= arrayListOf<Image42Bean>()
    var list2= arrayListOf<Image42Bean>()
    var list3= arrayListOf<Image42Bean>()
    var listAll= arrayListOf<Image42Bean>()
    var adapter: ImageAdapter24?=null
    var adapter2: ImageAdapter24?=null
    var adapter3: ImageAdapter24?=null

    var isAll=0
    var gson= Gson()
    var ids=0
    override fun initData() {
        ids= intent.getIntExtra("ids",0)
        vm.getData("id" to ids)
        adapter= ImageAdapter24(list)
        adapter2= ImageAdapter24(list2)
        adapter3= ImageAdapter24(list3)
        binding.apply {
            tvAll.clickWithTrigger {
                if (isAll==0){
                    tvAll.text="取消全选"
                    isAll=1
                    for (b in list3){
                        b.isCheck=1
                    }
                    for (b in list){
                        b.isCheck=1
                    }
                    for (b in list2){
                        b.isCheck=1
                    }
                    adapter?.notifyDataSetChanged()
                    adapter2?.notifyDataSetChanged()
                    adapter3?.notifyDataSetChanged()
                }else{
                    isAll=0
                    tvAll.text="全选"
                    for (b in list3){
                        b.isCheck=0
                    }
                    for (b in list){
                        b.isCheck=0
                    }
                    for (b in list2){
                        b.isCheck=0
                    }
                    adapter?.notifyDataSetChanged()
                    adapter2?.notifyDataSetChanged()
                    adapter3?.notifyDataSetChanged()
                }
            }
            tvShare.clickWithTrigger {
                listAll.clear()
                for (b in list){
                    if (b.isCheck==1)
                    listAll.add(b)
                }
                for (b in list2){
                    if (b.isCheck==1)
                        listAll.add(b)
                }
                for (b in list3){
                    if (b.isCheck==1)
                        listAll.add(b)
                }
                if (listAll.size>0){
                    var content=""
                    for (b in listAll){
                        content=content+b.image+","
                    }
                    vm.getData2("id" to ids,"content"  to content.substring(0,content.length-1))

                }else{
                    toast("请选择图片")
                }



            }
            rvImage.layoutManager= GridLayoutManager(this@ServiceLog3Activity,4)
            rvImage2.layoutManager= GridLayoutManager(this@ServiceLog3Activity,4)
            rvImage3.layoutManager= GridLayoutManager(this@ServiceLog3Activity,4)
            rvImage.adapter=adapter
            rvImage2.adapter=adapter2
            rvImage3.adapter=adapter3
            adapter?.setOnItemClick { view, position,_ ->
                if (list[position].isCheck==0){
                    list[position].isCheck=1
                    setAll()
                }else{
                    list[position].isCheck=0
                    setAll()
                }
                adapter?.notifyDataSetChanged()
            }
            adapter3?.setOnItemClick { view, position,_ ->
                if (list3[position].isCheck==0){
                    list3[position].isCheck=1
                    setAll()
                }else{
                    list3[position].isCheck=0
                    setAll()
                }
                adapter3?.notifyDataSetChanged()
            }
            adapter2?.setOnItemClick { view, position,_ ->
                if (list2[position].isCheck==0){
                    list2[position].isCheck=1
                    setAll()
                }else{
                    list2[position].isCheck=0
                    setAll()
                }
                adapter2?.notifyDataSetChanged()
            }
        }
    }

    private fun setIamge(iamge:String) {

    }


    private fun setAll() {
        var isAll1 = 1
        for (b in list) {
            if (b.isCheck == 0) {
                isAll1 = 0
            }
        }
        for (b in list2) {
            if (b.isCheck == 0) {
                isAll1 = 0
            }
        }
        for (b in list3) {
            if (b.isCheck == 0) {
                isAll1 = 0
            }
        }
        if (isAll1 == 1) {
            isAll = 1
            binding.tvAll.text = "取消全选"
        }else{
            isAll = 0
            binding.tvAll.text = "全选"
        }
    }

    override fun startObserve() {
        vm.getData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                if (code!="402"){
                    toast(msg)
                }
            }
            onSuccess {
                dismissLoading()
                if (it!=null){
                    list.clear()

                    list2.clear()
                    list3.clear()

                    if (!it.before_service_img.isNullOrBlank()){
                        var arr=it.before_service_img.split(",")
                        for (b in arr){
                            var bean = Image42Bean(b, 0, 0)
                            list.add(bean)
                        }



                    }
                    if (!it.before_service_video.isNullOrBlank()){
                        var bean2 = Image42Bean(it.before_service_video, 1, 0)
                        list.add(bean2)
                    }
                    if (!it.service_img.isNullOrBlank()){
                        var arr=it.service_img.split(",")
                        for (b in arr){
                            var bean = Image42Bean(b, 0, 0)
                            list3.add(bean)
                        }


                    }
                    if (!it.service_video.isNullOrBlank()){
                        var bean2 = Image42Bean(it.service_video, 1, 0)
                        list3.add(bean2)
                    }
                    if (!it.after_service_img.isNullOrBlank()){
                        var arr=it.after_service_img.split(",")
                        for (b in arr){
                            var bean = Image42Bean(b, 0, 0)
                            list2.add(bean)
                        }


                    }
                    if (!it.after_service_video.isNullOrBlank()){
                        var bean2 = Image42Bean(it.after_service_video, 1, 0)
                        list2.add(bean2)
                    }
                    adapter?.notifyDataSetChanged()
                    adapter2?.notifyDataSetChanged()
                    adapter3?.notifyDataSetChanged()
                }
            }
        }
        vm.getData2.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                if (code!="402"){
                    toast(msg)
                }
            }
            onSuccess {
                dismissLoading()
                if (it!=null){
                    var str=gson.toJson(listAll)
                    startPage<ServiceLog4Activity>("bean" to str,"ids" to it.id)
                }
            }
        }
    }

}